package leeon.bbsbrower.views;

import ipworks.IPWorksException;
import leeon.bbsbrower.actions.BBSLogAction;
import leeon.bbsbrower.actions.BBSMsgAction;
import leeon.bbsbrower.actions.Config;
import leeon.bbsbrower.models.FootObject;

import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;

/**
 * An action bar advisor is responsible for creating, adding, and disposing of
 * the actions added to a workbench window. Each window will be populated with
 * new actions.
 */
public class ApplicationActionBarAdvisor extends ActionBarAdvisor {

	// Actions - important to allocate these only in makeActions, and then use
	// them
	// in the fill methods. This ensures that the actions aren't recreated
	// when fillActionBars is called with FILL_PROXY.
	public static IWorkbenchAction exitAction;
	
	private static IStatusLineManager slm = null;
	private static String slmmsg = null;

	public ApplicationActionBarAdvisor(final IActionBarConfigurer configurer) {
		super(configurer);
		
		slm = configurer.getStatusLineManager();
		
		//状态查看线程
		Thread thread1 = new Thread(new Runnable() {
			public void run() {
				while (true) {
					try {						
						FootObject foot = BBSLogAction.foot();
						if (foot != null) {
							slmmsg = foot.toString();
							//boolean newMail = (foot.getMailNumber() != null && foot.getMailNumber().indexOf("(") != -1);
							//设定状态
							Display.getDefault().asyncExec(new Runnable() {
								public void run() {
									setStatusLine();
								}										
							});
						}
						Thread.sleep(Config.STATUS_REFRESH*1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					} catch (IPWorksException e) {
						e.printStackTrace();
					}
				}
			}			
		});
		thread1.start();
		
		//msg查看线程
		Thread thread2 = new Thread(new Runnable() {
			public void run() {
				while (true) {
					try {
						Thread.sleep(Config.MSG_REFRESH*1000);
						
						final String[] msgs = BBSMsgAction.getNewMsg();						
						if (msgs != null) {
						//弹出msg对话筐
							Display.getDefault().syncExec(new Runnable() {
								public void run() {
									MsgDialog msg = new MsgDialog(
											configurer.getWindowConfigurer().getWindow().getShell(), 
											msgs[1], msgs[0], msgs[2]);
									msg.open();
								}										
							});
						}						
					} catch (InterruptedException e) {
						e.printStackTrace();
					} catch (IPWorksException e) {
						e.printStackTrace();
					}
				}
			}			
		});
		thread2.start();
	}
	
	/**
	 * 设定状态栏的信息
	 *
	 */
	static void setStatusLine() {
		slm.setMessage(slmmsg);
	}
	
	public static void setStatusLine(String msg) {
		slmmsg = msg;
	}

	protected void makeActions(final IWorkbenchWindow window) {
		// Creates the actions and registers them.
		// Registering is needed to ensure that key bindings work.
		// The corresponding commands keybindings are defined in the plugin.xml
		// file.
		// Registering also provides automatic disposal of the actions when
		// the window is closed.

		exitAction = ActionFactory.QUIT.create(window);
		register(exitAction);
	}

	protected void fillMenuBar(IMenuManager menuBar) {
		MenuManager fileMenu = new MenuManager("设置", IWorkbenchActionConstants.M_FILE);
		menuBar.add(fileMenu);
		fileMenu.add(exitAction);
	}

}
